/**
 *  Copyright (c) 1997-2013, www.tinygroup.org (luo_guo@icloud.com).
 *
 *  Licensed under the GPL, Version 3.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *       http://www.gnu.org/licenses/gpl.html
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package org.tinygroup.dbf.impl;

import org.tinygroup.dbf.DbfDataSet;
import org.tinygroup.dbf.DbfRecord;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * Created by luoguo on 2014/7/10.
 * 
 */
public class MemoryDbfDataSet implements DbfDataSet {
    private List<DbfRecord> dbfRecords = new ArrayList<DbfRecord>();
    private int index = -1;

    public List<DbfRecord> getDbfRecords() {
        return dbfRecords;
    }

    public void setDbfRecords(List<DbfRecord> dbfRecords) {
        this.dbfRecords = dbfRecords;
    }

    public void moveBeforeFirst() throws IOException {
        index = -1;
    }

    public boolean next() {
        if (index + 1 < dbfRecords.size()) {
            index++;
            return true;
        }
        return false;
    }

    public boolean absolute(int position) {
        if (position >= 1 && position <= dbfRecords.size()) {
            index = position - 1;
            return true;
        }
        return false;
    }

    public DbfRecord getDbfRecord() throws IOException { 
        if (index < 0 || index >= dbfRecords.size()) {
            throw new IOException("当前记录位置为:" + index + ",不是合法的记录位置。");
        }
        return dbfRecords.get(index);
    }

	public int getDbfRecordCount() {
		return dbfRecords.size();
	}
}
